#include "TUserDal.h"
#include "Dal.h"

void TUser_Add(TUser * model)
{
	if(model == NULL)
	{
		printf("the model is null!\n");
		return;	
	}

	MYSQL * conn;
	conn = mysql_init(NULL);
	conn = mysql_real_connect(conn, TIP, "root", "9904wcnm", "DBGameServer", 0, NULL, 0);
	MYSQL_STMT * stmt = mysql_stmt_init(conn);
	// insert into TUser values(uuid(), 'z1619', 'z1619', 2)
	char * sql = "insert into TUser values(uuid(), ?, ?, ?)"; 	
	if(mysql_stmt_prepare(stmt, sql, strlen(sql)))
	{
		printf("prepare err");
	}
	char inuname[1024] = {0};
	char inupwd[1024] = {0};
	int inage = 0;
	strcpy(inuname, model->TU_UserName);
	strcpy(inupwd, model->TU_Password); 
	inage = model->TU_Age;
	MYSQL_BIND inbind[3] = {0};
	inbind[0].buffer_type = MYSQL_TYPE_STRING;
	inbind[0].buffer = inuname;
	inbind[0].buffer_length = strlen(inuname);
	inbind[1].buffer_type = MYSQL_TYPE_STRING;
	inbind[1].buffer = inupwd;
	inbind[1].buffer_length = strlen(inupwd);
	inbind[2].buffer_type = MYSQL_TYPE_LONG;
	inbind[2].buffer = &inage;
	mysql_stmt_bind_param(stmt, inbind);
	//starttran(conn);
	setauto0(conn);
	mysql_stmt_execute(stmt);
	commit(conn); // <==================== 相当于EF中的SaveChanges
	//commit(conn);
	//rollback(conn);
	int ir = mysql_stmt_affected_rows(stmt);
	printf("影响行数：%d\n", ir);	
	mysql_stmt_close(stmt);
	mysql_close(conn);	

}

void TUser_paraquery2(TUser *(*parr)[1024])
{

	MYSQL * conn;
	//1. init mysql
	conn = mysql_init(NULL);
	//2. real_connect
	conn = mysql_real_connect(conn, TIP, "root", "9904wcnm", "DBGameServer", 0, NULL, 0);

	if(parr == NULL)
	{
		printf("parr == NULL\n");
		return;
	}


	// 1.create stmt 
	MYSQL_STMT * stmt = mysql_stmt_init(conn);

	// 2.create sql
	char * sql = "select * From TUser where TU_Age = ?";	
	// 3.prepare stmt
	if(mysql_stmt_prepare(stmt, sql, strlen(sql)))
	{
		printf("prepare err\n");
		return;
	}		

	// 4.prepare inBinds and outBinds
	char guid[36] = {0};
	char uname[255] = {0};
	char upwd[255] = {0};
	int age = 0;
	//char inuname[255] = {0};
	int iage = 2;
	//strcpy(inuname, "admin");
	MYSQL_BIND inbind[1];
	MYSQL_BIND outbind[4];
	memset(inbind, 0, sizeof(inbind));	
	memset(outbind, 0, sizeof(outbind));
	inbind[0].buffer_type = MYSQL_TYPE_LONG;
	inbind[0].buffer = &iage;
	outbind[0].buffer_type = MYSQL_TYPE_STRING;
	outbind[0].buffer = guid;
	outbind[0].buffer_length = sizeof(guid);
	outbind[1].buffer_type = MYSQL_TYPE_STRING;
	outbind[1].buffer = uname;
	outbind[1].buffer_length = sizeof(uname);
	outbind[2].buffer_type = MYSQL_TYPE_STRING;
	outbind[2].buffer = upwd;
	outbind[2].buffer_length = sizeof(upwd);
	outbind[3].buffer_type = MYSQL_TYPE_LONG;
	outbind[3].buffer = &age;
	
	// 5.bind
	mysql_stmt_bind_param(stmt, inbind);
	mysql_stmt_bind_result(stmt, outbind);
	mysql_stmt_execute(stmt);
	mysql_stmt_store_result(stmt);

	// 6.fetch
	int i = 0;
	while(mysql_stmt_fetch(stmt) == 0)
	{
		/*TUser u;
		memset(&u, 0, sizeof(TUser));
		strcpy(u.guid, guid);
		strcpy(u.uname, uname);
		strcpy(u.upwd, upwd);
		u.age = age;*/

		TUser * u = (TUser *)calloc(1, sizeof(TUser));
		(*parr)[i] = u;		
		strcpy(u->TU_Guid, guid);	
		strcpy(u->TU_UserName, uname);	
		strcpy(u->TU_Password, upwd);	
		u->TU_Age = age;
		i++;
	}
	(*parr)[i] = NULL;	

	// 7.close stmt
	mysql_stmt_close(stmt);
	mysql_close(conn);
}

void TUser_GetFirstOrDefaultByTU_UserName(const char * const tu_username, TUser ** destUser)
{

	if(tu_username == NULL || destUser == NULL)
	{
		printf("parameter err!\n");
		return;
	}

	MYSQL * conn;
	//1. init mysql
	conn = mysql_init(NULL);
	//2. real_connect
	conn = mysql_real_connect(conn, TIP, "root", "9904wcnm", "DBGameServer", 0, NULL, 0);

	// 1.create stmt 
	MYSQL_STMT * stmt = mysql_stmt_init(conn);

	// 2.create sql
	// select * From TUser where TU_UserName = 'zjr11111' LIMIT 1
	char * sql = "select * From TUser where TU_UserName = ? LIMIT 1";	
	// 3.prepare stmt
	if(mysql_stmt_prepare(stmt, sql, strlen(sql)))
	{
		printf("prepare err\n");
		return;
	}		

	// 4.prepare inBinds and outBinds
	char guid[1024] = {0};
	char uname[1024] = {0};
	char upwd[1024] = {0};
	int age = 0;
	//char inuname[255] = {0};
	//int iage = 2;
	//strcpy(inuname, "admin");
	char inuname[1024] = {0};
	strcpy(inuname, tu_username);
	// para 1
	MYSQL_BIND inbind[1];
	// out 4
	MYSQL_BIND outbind[4];
	memset(inbind, 0, sizeof(inbind));	
	memset(outbind, 0, sizeof(outbind));
	inbind[0].buffer_type = MYSQL_TYPE_STRING;
	inbind[0].buffer = inuname;
	inbind[0].buffer_length = strlen(inuname);
	outbind[0].buffer_type = MYSQL_TYPE_STRING;
	outbind[0].buffer = guid;
	outbind[0].buffer_length = sizeof(guid);
	outbind[1].buffer_type = MYSQL_TYPE_STRING;
	outbind[1].buffer = uname;
	outbind[1].buffer_length = sizeof(uname);
	outbind[2].buffer_type = MYSQL_TYPE_STRING;
	outbind[2].buffer = upwd;
	outbind[2].buffer_length = sizeof(upwd);
	outbind[3].buffer_type = MYSQL_TYPE_LONG;
	outbind[3].buffer = &age;
	
	// 5.bind
	mysql_stmt_bind_param(stmt, inbind);
	mysql_stmt_bind_result(stmt, outbind);
	mysql_stmt_execute(stmt);
	mysql_stmt_store_result(stmt);

	// 6.fetch
	printf("即将显示是否有结果\n");
	if(mysql_stmt_fetch(stmt) == 0)
	{
		printf("有结果！\n");
		/*TUser u;
		memset(&u, 0, sizeof(TUser));
		strcpy(u.guid, guid);
		strcpy(u.uname, uname);
		strcpy(u.upwd, upwd);
		u.age = age;*/

		TUser * u = (TUser *)calloc(1, sizeof(TUser));
		*destUser = u;		
		strcpy(u->TU_Guid, guid);	
		strcpy(u->TU_UserName, uname);	
		strcpy(u->TU_Password, upwd);	
		u->TU_Age = age;
	}

	// 7.close stmt
	mysql_stmt_close(stmt);
	mysql_close(conn);
}
